<template>
  <el-container style="margin-bottom: 30px">
    <el-aside style="margin-left: 10%">
      <span>{{ nameZH }}</span>
      <div class="block">
        <el-image :src="require(`@/assets/img/${src}`)" fit="fill" style="width: 150px; height: 150px"></el-image>
      </div>
      <el-button type="primary" v-loading.body.lock="fullscreenLoading" @click="serverStart" style="margin-top: 10px">开启</el-button>
      <el-button type="danger" @click="serverStop">停止</el-button>
    </el-aside>
    <el-main style="margin-top: 1%;margin-left: 10%">
      <el-form ref="form" label-width="auto">
        <el-form-item label="IP地址:">
          <span>{{ serverInfo.ip }}</span>
        </el-form-item>
        <el-form-item label="端口地址:">
          <span>{{ serverInfo.port }}</span>
        </el-form-item>
        <el-form-item label="是否已经开启服务:">
          <span>{{ serverInfo.authStatus }}</span>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import { start, stop } from '@/api/server-status'

export default {
  name: 'ServerComponent',
  // eslint-disable-next-line vue/require-prop-types
  props: ['serverName'],
  data() {
    return {
      serverInfo: {
        ip: 'unknown',
        port: 'unknown',
        authStatus: '否'
      },
      fullscreenLoading: false
    }
  },
  // created() {
  //   this.serverStart()
  // },
  computed: {
    src() {
      if (this.serverName === 'adaptive') return 'server1.png'
      else if (this.serverName === 'user') return 'server1.png'
      else return 'server1.png'
    },
    nameZH() {
      if (this.serverName === 'adaptive') return '4/5G自适应认证服务器'
      else if (this.serverName === 'user') return '用户认证服务器'
      else return '(软)生物认证服务器'
    }
  },
  methods: {
    serverStart() {
      this.fullscreenLoading = true
      // 向后台发送开启指令，并获取ip、port和其他信息，可以使用自定义事件完成（不熟），这里还是通过调用API来完成吧
      start(this.serverName).then(response => {
        // this.serverInfo = response.data.item
        setTimeout(() => {
          this.serverInfo.ip = response.data.ip
          this.serverInfo.port = response.data.port
          this.serverInfo.authStatus = '认证已开启'
          this.fullscreenLoading = false
          this.$notify({
            title: 'Success',
            message: 'Server start successfully!',
            type: 'success',
            duration: 2000
          })
        }, 500)
      })
    },
    serverStop() {
      // 向后台发送关闭指令，并将页面中的ip、port等信息恢复
      stop(this.serverName).then(response => {
        setTimeout(() => {
          this.serverInfo.ip = 'unknown'
          this.serverInfo.port = 'unknown'
          this.serverInfo.authStatus = '否'
          this.$notify({
            title: 'Success',
            message: 'Stop Successfully',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
